package com.helsinki.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.helsinki.mapper.ProductMapper;
import com.helsinki.pojo.Product;
import com.helsinki.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public Product queryByPK(String id) {
        return productMapper.findById(id);
    }

    @Override
    public PageInfo<Product> queryAll(Integer pageNum, Integer pageSize) {
        // 只有在要分页的查询之前才能设置分页信息
        PageHelper.startPage(pageNum, pageSize);
        List<Product> list = productMapper.findAll();
        // PageInfo 封装了所有的分页信息
        return new PageInfo<>(list);
    }

    @Override
    public void addProduct(Product product) {
        UUID uuid = UUID.randomUUID();
        product.setId(uuid.toString().replaceAll("\\-", ""));

        productMapper.addProduct(product);
    }

    @Override
    public void updateProduct(Product product) {
        productMapper.updateById(product);
    }

    @Override
    public void deleteProduct(String id) {
        productMapper.closeForeignKey();
        productMapper.deleteById(id);
        productMapper.openForeignKey();
    }

    @Override
    public void deleteSelectP(String[] ids) {
        if (ids != null && ids.length > 0) {
            for (String id : ids) {
                deleteProduct(id);
            }
        }
    }

    @Override
    public void updateStatus(String[] ids, int status) {
        if (ids != null && ids.length > 0) {
            for (String id : ids) {
                productMapper.updateStatus(id, status);
            }
        }
    }


}
